package ui.fragment.minefragment;

import android.app.Dialog;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import Dailog.CommomDialog;
import base.BaseBean;
import base.fragment.BaseNetFragment;
import bean.ItemBean;
import bean.PersonInfoBean;
import bean.RegisterBean;
import bean.SchoolInfoBean;
import student.andlei.com.student.R;
import ui.fragment.MineFragment;
import utils.GsonUtils;
import utils.SPUtils;
import view.BottomListPopWindow;

/**
 * 所属学校
 */
public class SelectSchoolFragment extends BaseNetFragment {
    private ImageView img_back;
    private View layoutview;
    private TextView tv_school,
            tv_grade_class,
            tv_select_gradeclass;
    private Button bt_updateschool,
            bt_updategradeclass,
            bt_submit;
    private LinearLayout layout_update;
    private LinearLayout layout_school;
    private EditText ed_school;
    private SchoolInfoBean infoBean;
    private List<ItemBean> list = new ArrayList<>();
    private List<RegisterBean.School_info> lists = new ArrayList<>();
    private int schedule_id;
    private int school_id;
    private TextView tv_fragment_title;
    public static Fragment getInstance() {
        return new SelectSchoolFragment();
    }

    @Override
    public void success(String action, BaseBean baseBean) {
        if (baseBean.code == 200) {
            if (action.equals("personal/getSchoolByInvite")) {
                infoBean = (SchoolInfoBean) GsonUtils.JSONToObject(GsonUtils.beanToJSONString(baseBean.data), SchoolInfoBean.class);
                lists = GsonUtils.getInstance(context).parseHaveHeaderJArray(baseBean, RegisterBean.School_info.class, "class");
                school_id = infoBean.school_id;
                if (updateList2(lists).size() <= 0) {
                    toast("暂无班级信息");
                    return;
                }
                final BottomListPopWindow popWindow2 = new BottomListPopWindow(getActivity(), updateList2(lists));
                popWindow2.showAtBottom(layoutview);
                popWindow2.getAdapter().setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        schedule_id = list.get(position).schedule_id;
                        tv_select_gradeclass.setText(list.get(position).str);
                        popWindow2.dismiss();
                    }
                });
            } else if (action.equals("personal/joinSchool")) {
                SPUtils.getInstance(context).put("school_status", 10);
                layout_school.setVisibility(View.VISIBLE);
                SPUtils.getInstance(context).put("school_id",infoBean.school_id);
                tv_school.setText("学校："+infoBean.school_name);
                tv_grade_class.setText("班级："+tv_select_gradeclass.getText().toString());
            } else if (action.equals("personal/editSchoolAndClass")) {
                // personal/editSchoolAndClass
                layout_update.setVisibility(View.GONE);
                SPUtils.getInstance(context).put("school_id",school_id);
                loadData();
//                tv_school.setText("学校："+infoBean.school_name);
//                tv_grade_class.setText("班级："+tv_select_gradeclass.getText().toString());
            } else  if(action.equals("personal/getInfo")){
                PersonInfoBean Bean = (PersonInfoBean) GsonUtils.JSONToObject(GsonUtils.beanToJSONString(baseBean.data), PersonInfoBean.class);
                tv_school.setText("学校："+Bean.school_name);
                tv_grade_class.setText("班级："+Bean.schedule_name);

            }else if(action.equals("personal/getClass")){
                lists = GsonUtils.getInstance(context).parseNoHeaderJArray(baseBean,RegisterBean.School_info.class);
                if (updateList2(lists).size() <= 0) {
                    toast("暂无班级信息");
                    return;
                }
                final BottomListPopWindow popWindow2 = new BottomListPopWindow(getActivity(), updateList2(lists));
                popWindow2.showAtBottom(layoutview);
                popWindow2.getAdapter().setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        schedule_id = list.get(position).schedule_id;
                        tv_select_gradeclass.setText(list.get(position).str);
                        popWindow2.dismiss();
                    }
                });
            }
        } else {
            toast(baseBean.msg);
        }
    }

    @Override
    public void error(String action, Throwable e) {

    }

    @Override
    protected int getLayoutId() {
        layoutview = View.inflate(context, R.layout.fragment_selectschool, null);
        return R.layout.fragment_selectschool;
    }

    @Override
    protected void findViews() {
        img_back = getView(R.id.img_back);
        tv_school = getView(R.id.tv_school);
        tv_grade_class = getView(R.id.tv_grade_class);
        tv_select_gradeclass = getView(R.id.tv_select_grade);
        bt_updateschool = getView(R.id.bt_updateschool);
        bt_updategradeclass = getView(R.id.bt_updategrade_class);
        bt_submit = getView(R.id.bt_submit);
        layout_update = getView(R.id.layout_update);
        layout_school = getView(R.id.layout_school);
        ed_school = getView(R.id.ed_school);
        tv_fragment_title = getView(R.id.tv_fragment_title);
    }

    @Override
    protected void formatViews() {
        img_back.setOnClickListener(this);
        bt_updategradeclass.setOnClickListener(this);
        bt_updateschool.setOnClickListener(this);
        bt_submit.setOnClickListener(this);
        tv_select_gradeclass.setOnClickListener(this);
    }

    @Override
    protected void formatData() {
        loadData();
        try{
            if (SPUtils.getInstance(context).getInt("school_status") == 0) {
                layout_school.setVisibility(View.GONE);
            } else {
                layout_update.setVisibility(View.GONE);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private void loadData() {
        post("personal/getInfo", BaseBean.class, false);
    }

    @Override
    protected void getBundle() {
        school_id = SPUtils.getInstance(context).getInt("school_id");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                ((MineFragment) (this.getParentFragment())).GojumpPersoninfoFragment();
                break;
            //提交
            case R.id.bt_submit:
                if (SPUtils.getInstance(context).getInt("school_status") == 0) {
                    Map<String, String> smap = new HashMap<>();
                    if(school_id <=0){toast("请先输入学校邀请码"); return;}
                    smap.put("school_id",school_id + "");
                    smap.put("schedule_id", schedule_id + "");
                    post("personal/joinSchool", smap, BaseBean.class, false);
                } else {
                    Map<String, String> smap = new HashMap<>();
                    if(school_id <=0){toast("请先输入学校邀请码"); return;}
                    smap.put("school_id", school_id+ "");
                    smap.put("schedule_id", schedule_id + "");
                    post("personal/editSchoolAndClass", smap, BaseBean.class, false);
                }
                break;
            case R.id.tv_select_grade:
                if(school_id>0&&ed_school.getText().toString().length()<=0){
                    Map<String, String> map = new HashMap<>();
                    map.put("school_id", school_id+"");
                    post("personal/getClass",map,BaseBean.class, false);
                }else {
                    Map<String, String> map = new HashMap<>();
                    map.put("code", ed_school.getText().toString().trim());
                    post("personal/getSchoolByInvite", map, BaseBean.class, false);
                }
                break;
            //修改学校
            case R.id.bt_updateschool:
                new CommomDialog(getActivity(), R.style.dialog, "加入新的学校后，之前的学校将退出并清空数据，确定要修改学校信息吗？", new CommomDialog.OnCloseListener() {
                    @Override
                    public void onClick(Dialog dialog, boolean confirm) {
                        if (confirm) {
                            layout_update.setVisibility(View.VISIBLE);
                            ed_school.setVisibility(View.VISIBLE);
                            dialog.dismiss();
                        }
                    }
                }).setTitle("提示").settPositiveButtonColor(R.color.homepage_titlefont).show();
                //tv_select_gradeclass.setVisibility(View.GONE);
                break;
            //修改班级
            case R.id.bt_updategrade_class:
                layout_update.setVisibility(View.VISIBLE);
                tv_select_gradeclass.setVisibility(View.VISIBLE);
                ed_school.setVisibility(View.GONE);
                break;
        }
    }

    private List<ItemBean> updateList2(List<RegisterBean.School_info> mlist) {
        list.clear();
        if (mlist != null) {
            for (RegisterBean.School_info info : mlist) {

                ItemBean bean = new ItemBean();
                bean.setStr(info.schedule_name);

                bean.setSchedule_id(info.schedule_id);
                list.add(bean);

            }
        }
        return list;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            loadData();
            if (SPUtils.getInstance(context).getInt("school_status") == 0) {
                layout_school.setVisibility(View.GONE);
            } else {
                layout_update.setVisibility(View.GONE);
            }
            if(((MineFragment)(this.getParentFragment())).Updatetype.equals("学校")){
                tv_fragment_title.setText("所属学校");
                bt_updateschool.setVisibility(View.VISIBLE);
                bt_updategradeclass.setVisibility(View.GONE);
            }else {
                tv_fragment_title.setText("所属班级");
                bt_updateschool.setVisibility(View.GONE);
            }
        }
    }
}
